package domain.entities;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 02/11/12
 * Time: 12:36
 * To change this template use File | Settings | File Templates.
 */
public class GlobalCommitmentDAO extends HibernateDaoSupport {
    private static final Logger log = LoggerFactory
            .getLogger(GlobalCommitmentDAO.class);
    // property constants

    protected void initDao() {
        // do nothing
    }

    public void save(GlobalCommitment transientInstance) {
        log.debug("saving GlobalCommitment instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(GlobalCommitment persistentInstance) {
        log.debug("deleting GlobalCommitment instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public GlobalCommitment findById(java.lang.Integer id) {
        log.debug("getting GlobalCommitment instance with id: " + id);
        try {
            GlobalCommitment instance = (GlobalCommitment) getHibernateTemplate().get(
                    "domain.entities.GlobalCommitment", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<GlobalCommitment> findByExample(GlobalCommitment instance) {
        log.debug("finding GlobalCommitment instance by example");
        try {
            List<GlobalCommitment> results = (List<GlobalCommitment>) getHibernateTemplate()
                    .findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding GlobalCommitment instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from GlobalCommitment as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findAll() {
        log.debug("finding all GlobalCommitment instances");
        try {
            String queryString = "from GlobalCommitment";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public GlobalCommitment merge(GlobalCommitment detachedInstance) {
        log.debug("merging GlobalCommitment instance");
        try {
            GlobalCommitment result = (GlobalCommitment) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(GlobalCommitment instance) {
        log.debug("attaching dirty GlobalCommitment instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(GlobalCommitment instance) {
        log.debug("attaching clean GlobalCommitment instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static GlobalCommitmentDAO getFromApplicationContext(
            ApplicationContext ctx) {
        return (GlobalCommitmentDAO) ctx.getBean("GlobalCommitmentDAO");
    }

    public List<GlobalCommitment> findByProtocoloId(String id) {
        log.debug("finding all ProtocoloCentro instances");
        try {
            String queryString = "from GlobalCommitment a where protocolo_id = '" + id + "'";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
}